﻿using System;
using NopSolutions.NopCommerce.BusinessLogic;
using NopSolutions.NopCommerce.BusinessLogic.Infrastructure;
using NopSolutions.NopCommerce.BusinessLogic.Model;
using NopSolutions.NopCommerce.BusinessLogic.Services;

namespace NopSolutions.NopCommerce.Web.Modules
{
    public partial class NewsLetterSubscriptionBoxControl : BaseNopFrontendUserControl
    {
        private ISettingService _settingService;
        private IMessageService _messageService;

        public NewsLetterSubscriptionBoxControl() {
            _settingService = IoC.Resolve<ISettingService>();
            _messageService = IoC.Resolve<IMessageService>();
        }

        protected override void OnInit(EventArgs e) {
            Visible = !_settingService.GetSettingValueBoolean("Display.HideNewsletterBox");
            btnSubscribe.Click += new EventHandler(BtnSubscribe_OnClick);

            base.OnInit(e);
        }

        protected void BtnSubscribe_OnClick(object sender, EventArgs e) {
            try {
                if (Page.IsValid) {
                    var subscription = _messageService.GetNewsLetterSubscriptionByEmail(txtNewsletterEmail.Text);
                    if (subscription != null) {
                        if (rbSubscribe.Checked) {
                            if (!subscription.Active) {
                                _messageService.SendNewsLetterSubscriptionActivationMessage(subscription.NewsLetterSubscriptionId, NopContext.Current.WorkingLanguage.LanguageId);
                            }
                            pnlResult.Attributes.Add("class", "alert alert-success");
                            lblResult.Text = GetLocaleResourceString("NewsLetterSubscriptionBox.SubscriptionCreated");
                        } else if (rbUnsubscribe.Checked) {
                            if (subscription.Active) {
                                _messageService.SendNewsLetterSubscriptionDeactivationMessage(subscription.NewsLetterSubscriptionId, NopContext.Current.WorkingLanguage.LanguageId);
                            }
                            pnlResult.Attributes.Add("class", "alert alert-error");
                            lblResult.Text = GetLocaleResourceString("NewsLetterSubscriptionBox.SubscriptionDeactivated");
                        }
                    } else if (rbSubscribe.Checked) {
                        subscription = new NewsLetterSubscription() {
                            NewsLetterSubscriptionGuid = Guid.NewGuid(),
                            Email = txtNewsletterEmail.Text,
                            Active = false,
                            CreatedOn = DateTime.UtcNow
                        };
                        _messageService.InsertNewsLetterSubscription(subscription);
                        _messageService.SendNewsLetterSubscriptionActivationMessage(subscription.NewsLetterSubscriptionId, NopContext.Current.WorkingLanguage.LanguageId);
                        pnlResult.Attributes.Add("class", "alert alert-success");
                        lblResult.Text = GetLocaleResourceString("NewsLetterSubscriptionBox.SubscriptionCreated");
                    } else {
                        pnlResult.Attributes.Add("class", "alert alert-error");
                        lblResult.Text = GetLocaleResourceString("NewsLetterSubscriptionBox.SubscriptionDeactivated");
                    }
                } else {
                    pnlResult.Attributes.Add("class", "alert alert-error");
                    lblResult.Text = GetLocaleResourceString("NewsLetterSubscriptionBox.Error");
                }
            } catch (Exception ex) {
                pnlResult.Attributes.Add("class", "alert alert-error");
                lblResult.Text = ex.Message;
            }

            pnlResult.Visible = true;
            pnlSubscribe.Visible = false;
        }
    }
}